package com.huffman.smart_picture.controller;

import cn.hutool.core.util.StrUtil;
import com.huffman.smart_picture.common.auth.AuthConstant;
import com.huffman.smart_picture.common.auth.annotation.CheckRole;
import com.huffman.smart_picture.common.auth.annotation.LoginRequired;
import com.huffman.smart_picture.common.UserSession;
import com.huffman.smart_picture.common.base.reponse.ResponseResult;
import com.huffman.smart_picture.common.exception.ErrorEnum;
import com.huffman.smart_picture.domain.constants.CommonConstant;
import com.huffman.smart_picture.domain.request.exchange.VipExchangeCreateRequest;
import com.huffman.smart_picture.domain.request.user.UserExchangeVipRequest;
import com.huffman.smart_picture.service.VipExchangeCodeService;
import com.huffman.smart_picture.utils.ResponseUtil;
import com.huffman.smart_picture.utils.ThrowUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/exchange")
public class ExchangeController {
    @Resource
    VipExchangeCodeService vipExchangeCodeService;

    @PostMapping("/create")
    @ApiOperation(value = "vip兑换码生成")
    @CheckRole(roles = {CommonConstant.ADMIN})
    public ResponseResult createVipExchangeCode(@RequestBody VipExchangeCreateRequest vipExchangeCreateRequest, HttpServletRequest httpServletRequest) {
        ThrowUtil.throwIf(vipExchangeCreateRequest == null, ErrorEnum.PARAM_EMPTY, "请求参数为空");
        UserSession userSession = (UserSession) httpServletRequest.getSession().getAttribute(AuthConstant.LOGIN_USER);
        return ResponseUtil.success(vipExchangeCodeService.createVipExchangeCode(vipExchangeCreateRequest, userSession));
    }


    @PostMapping("/vip")
    @ApiOperation(value = "vip兑换")
    @LoginRequired
    public ResponseResult exchangeVip(@RequestBody UserExchangeVipRequest userExchangeVipRequest, HttpServletRequest httpServletRequest) {
        ThrowUtil.throwIf(userExchangeVipRequest == null, ErrorEnum.PARAM_EMPTY, "请求参数为空");
        String code = userExchangeVipRequest.getCode();
        ThrowUtil.throwIf(StrUtil.isBlank(code), ErrorEnum.PARAM_EMPTY, "兑换码为空");
        return ResponseUtil.success(vipExchangeCodeService.exchangeVip(code, httpServletRequest));
    }
}
